package com.steven.realServer.service;

import com.alibaba.fastjson.JSONObject;
import com.steven.realServer.dao.UserInfoDao;
import com.steven.realServer.pojo.UserInfo;
import com.steven.realServer.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginServiceImp implements LoginService {

    @Autowired
    private UserInfoDao dao;

    public JSONObject login(UserInfo info) {
        List<UserInfo> list = dao.searchUser(info.getEmail());
        JSONObject result = new JSONObject();
        if (list.size() > 0) {
            if (info.getPassword() != null) {
                if (info.getPassword().equals(list.get(0).getPassword())) {
                    result.put("userId", list.get(0).getUid());
                    result.put("msg", "success");
                    result.put("canLogin", true);
                } else {
                    result.put("msg", "密码错误");
                    result.put("canLogin", false);
                }
                return ResultUtils.makeResult(0, result);
            }
        }
        result.put("canLogin", false);
        result.put("msg", "用户不存在");
        return ResultUtils.makeResult(0, result);
    }


    public JSONObject loginQuick(UserInfo info) {
        List<UserInfo> list = dao.searchUser(info.getEmail());
        JSONObject result = new JSONObject();
        if (list.size() > 0) {
            result.put("userId", list.get(0).getUid());
            result.put("canLogin", true);
            result.put("msg", "success");
        } else {
            if (info.getEmail() == null) {
                result.put("canLogin", false);
                result.put("msg", "邮箱不能为空");
            } else {
                int res = dao.insertUser(info.getEmail(), info.getPassword());
                List<UserInfo> userInfo = dao.searchUser(info.getEmail());
                if (userInfo.size() > 0) {
                    result.put("userId", userInfo.get(0).getUid());
                }
                if (res > 0) {
                    result.put("canLogin", true);
                    result.put("msg", "success");
                } else {
                    result.put("canLogin", false);
                    result.put("msg", "登录失败");
                }
            }
        }
        return ResultUtils.makeResult(0, result);
    }


    public JSONObject signUp(UserInfo info) {
        List<UserInfo> list = dao.searchUser(info.getEmail());
        JSONObject result = new JSONObject();
        if (list.size() > 0) {
            result.put("canSignUp", false);
            result.put("msg", "用户已存在");
        } else {
            if (info.getEmail() == null) {
                result.put("canSignUp", false);
                result.put("msg", "邮箱不能为空");
            } else {
                int res = dao.insertUser(info.getEmail(), info.getPassword());
                List<UserInfo> userInfoList = dao.searchUser(info.getEmail());
                if (res > 0) {
                    result.put("userId", userInfoList.get(0).getUid());
                    result.put("canSignUp", true);
                    result.put("msg", "success");
                } else {
                    result.put("canSignUp", false);
                    result.put("msag", "注册失败");
                }
            }
        }
        return ResultUtils.makeResult(0, result);
    }
}
